/***
This figure plots changes in employment by wage quartile.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Employment"

* Create required subfolders
cap mkdir "${root}/results/Employment"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml"

* Graph text size
local text_size = 2

*-------------------------------------------------------------------------------
* Import tracker series
*-------------------------------------------------------------------------------

* Use tracker series
project, uses("${root}/data/web/data/Employment - National - Weekly.csv")
import delimited "${root}/data/web/data/Employment - National - Weekly.csv", clear

* Create date variable
gen date = mdy(month, day_endofweek, year)
format date %td

* Drop extra variables
keep date emp_incq*

* Rescale
forvalues q = 1/4 {
	replace emp_incq`q' =  emp_incq`q' * 100
}

* Set locals
keep if date <= ${finaldate}
sum date
local min = `r(min)'
local max = `r(max)'

forval i = 1(1)12 {
 	local thisdate = mdy(`i', 1, 2020)
 	local mydates `mydates' `thisdate'
 }

 forval i = 1(1)`=month(`=$finaldate')+1' {
 	local thisdate = mdy(`i', 1, 2021)
 	local mydates `mydates' `thisdate'
 }

di "`max'"
local ces_jan = 127556000/4
foreach q in q1 q2 q3 q4 {

	sum emp_inc`q' if date == td(17apr2020)
	assert `r(N)' == 1
	local apr_`q' = round(`r(mean)', 1)
	local apr_jobs_`q' : di %4.1f `ces_jan' * (`r(mean)'/100) / 1000000
	local apr_jobs_`q' = ustrtrim("`apr_jobs_`q''")

	sum emp_inc`q' if date == `=$finaldate'
	assert `r(N)' == 1
	local predict_`q' = round(`r(mean)', 1)
	local predict_`q'_str = cond(`r(mean)' > 0, "+`predict_`q''%", "`predict_`q''%")
	local predicted_jobs_`q' : di %4.1f `ces_jan' * (`r(mean)'/100) / 1000000
	local predicted_jobs_`q' = ustrtrim("`predicted_jobs_`q''")

	sum emp_inc`q' if month(date) == 7 & year(date) == 2020
	local jul_`q': di %2.0f r(mean)
}

local apr = td(17apr2020)
local jun = td(15jun2020)

* Keep relevant dates
keep if date >= mdy(1, 15, 2020)

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

tw	(line emp_incq4 date, sort color(oi2)) ///
	(line emp_incq3 date, sort color(oi4)) ///
	(line emp_incq2 date, sort color(oi3)) ///
	(line emp_incq1 date, sort color(oi1)) ///
	(scatter emp_incq4 date if date == $finaldate | date == td(17apr2020), sort color(oi2)) ///
	(scatter emp_incq3 date if date == $finaldate | date == td(17apr2020), sort color(oi4)) ///
	(scatter emp_incq2 date if date == $finaldate | date == td(17apr2020), sort color(oi3)) ///
	(scatter emp_incq1 date if date == $finaldate | date == td(17apr2020), sort color(oi1)) ///
	, ///
	xlab(`=mdy(1, 1, 2020)' `""Jan" "2020""'  `=mdy(3, 1, 2020)' "Mar" ///
	 `=mdy(5, 1, 2020)' "May"  ///
	`=mdy(7, 1, 2020)' "Jul"  `=mdy(9, 1, 2020)' "Sep" ///
	 `=mdy(11, 1, 2020)' "Nov"  ///
	`=mdy(1, 1, 2021)' `""Jan" "2021""'   `=mdy(3, 1, 2021)' "Mar" ///
	 `=mdy(5, 1, 2021)' "May"  ///
	`=mdy(7, 1, 2021)' "Jul"  `=mdy(9, 1, 2021)' "Sep"  `=mdy(11, 1, 2021)' "Nov" ///
	,format(%tdm) labsize(small)) ///
	xtitle(" ") ${title_`version'} ///
	yline(0, lpattern(dash) lcolor(gs8)) ///
	b1title("     ", color(white) pos(3) ring(5) bmargin(0 0 200 0)) ///
	legend(order(1 "Top Wage Quartile"  2 "Third Quartile" 3 "Second Quartile" 4 "Bottom Wage Quartile") ///
	color(gs4) size(2.3) symxsize(*0.4) col(1) ring(0) pos(6) region(fcolor(white%0) lcolor(white%0))) ///
	ylabel(-40 "-40%" -30 "-30%"  -20 "-20%" -10 "-10%"  0 "0%" 10 "+10%", nogrid) yscale(range(-37.5 0))  ///
	ytitle("Change in Employment (%)" "Relative to January 2020") ///
	xline(`apr', lcolor(gs7)) ///
	xline(`=$finaldate', lcolor(gs7)) ///
	text(13 `=`apr'' "April 15 2020", color(gs7) size(2.5)) ///
	text(13 `=$finaldate + 50' "December 31 2021", place(9) color(gs7) size(2.5)) ///
	text(`=`predict_q1'' `=`=$finaldate'+31' "`predict_q1_str'" "(`predicted_jobs_q1'm)", color(oi1) size(`text_size')) ///
	text(`=`predict_q2' - 2' `=`=$finaldate'+31' "`predict_q2_str'" "(`predicted_jobs_q2'm)", color(oi3) size(`text_size')) ///
	text(`=`predict_q3' + 1' `=`=$finaldate'+31' "`predict_q3_str'" "(`predicted_jobs_q3'm)", color(oi4) size(`text_size')) ///
	text(`=`predict_q4' + 2' `=`=$finaldate'+31' "`predict_q4_str'" "(`predicted_jobs_q4'm)", color(oi2) size(`text_size')) ///
	text( `=`apr_q1'' `=`apr'-47' "`apr_q1'%" "(`apr_jobs_q1'm)", color(oi1) size(`text_size')) ///
	text( `=`apr_q2'' `=`apr'-47' "`apr_q2'%" "(`apr_jobs_q2'm)", color(oi3) size(`text_size')) ///
	text( `=`apr_q3'' `=`apr'-47' "`apr_q3'%" "(`apr_jobs_q3'm)", color(oi4) size(`text_size')) ///
	text( `=`apr_q4'' `=`apr'-47' "`apr_q4'%" "(`apr_jobs_q4'm" "jobs)", color(oi2) size(`text_size'))

oi_graph_export "${root}/results/Employment/Changes in Employment by Income Quartile - long", type(${fig_type})

* Black and white version for QJE
tw	(line emp_incq4 date, sort color(gs12)) ///
	(line emp_incq3 date, sort color(gs8)) ///
	(line emp_incq2 date, sort color(gs5)) ///
	(line emp_incq1 date, sort color(gs0)) ///
	(scatter emp_incq4 date if date == $finaldate | date == td(17apr2020), sort color(gs12)) ///
	(scatter emp_incq3 date if date == $finaldate | date == td(17apr2020), sort color(gs8)) ///
	(scatter emp_incq2 date if date == $finaldate | date == td(17apr2020), sort color(gs5)) ///
	(scatter emp_incq1 date if date == $finaldate | date == td(17apr2020), sort color(gs0)) ///
	, ///
	xlab(`=mdy(1, 1, 2020)' `""Jan" "2020""'  `=mdy(3, 1, 2020)' "Mar" ///
	 `=mdy(5, 1, 2020)' "May"  ///
	`=mdy(7, 1, 2020)' "Jul"  `=mdy(9, 1, 2020)' "Sep" ///
	 `=mdy(11, 1, 2020)' "Nov"  ///
	`=mdy(1, 1, 2021)' `""Jan" "2021""'   `=mdy(3, 1, 2021)' "Mar" ///
	 `=mdy(5, 1, 2021)' "May"  ///
	`=mdy(7, 1, 2021)' "Jul"  `=mdy(9, 1, 2021)' "Sep"  `=mdy(11, 1, 2021)' "Nov" ///
	,format(%tdm) labsize(small)) ///
	xtitle(" ") ${title_`version'} ///
	yline(0, lpattern(dash) lcolor(gs8)) ///
	b1title("     ", color(white) pos(3) ring(5) bmargin(0 0 200 0)) ///
	legend(order(1 "Top Wage Quartile"  2 "Third Quartile" 3 "Second Quartile" 4 "Bottom Wage Quartile") ///
	color(gs4) size(2.3) symxsize(*0.4) col(1) ring(0) pos(6) region(fcolor(white%0) lcolor(white%0))) ///
	ylabel(-40 "-40%" -30 "-30%"  -20 "-20%" -10 "-10%"  0 "0%" 10 "+10%", nogrid) yscale(range(-37.5 0))  ///
	ytitle("Change in Employment (%)" "Relative to January 2020") ///
	xline(`apr', lcolor(gs7)) ///
	xline(`=$finaldate', lcolor(gs7)) ///
	text(13 `=`apr'' "April 15 2020", color(gs7) size(2.5)) ///
	text(13 `=$finaldate + 50' "December 31 2021", place(9) color(gs7) size(2.5)) ///
	text(`=`predict_q1'' `=`=$finaldate'+35' "Q1: `predict_q1_str'" "(`predicted_jobs_q1'm)", color(gs0) size(`text_size')) ///
	text(`=`predict_q2' - 2' `=`=$finaldate'+35' "Q2: `predict_q2_str'" "(`predicted_jobs_q2'm)", color(gs0) size(`text_size')) ///
	text(`=`predict_q3' + 1' `=`=$finaldate'+35' "Q3: `predict_q3_str'" "(`predicted_jobs_q3'm)", color(gs0) size(`text_size')) ///
	text(`=`predict_q4' + 2' `=`=$finaldate'+35' "Q4: `predict_q4_str'" "(`predicted_jobs_q4'm)", color(gs0) size(`text_size')) ///
	text( `=`apr_q1'' `=`apr'-60' "Q1: `apr_q1'%" "(`apr_jobs_q1'm)", color(gs0) size(`text_size')) ///
	text( `=`apr_q2'' `=`apr'-60' "Q2: `apr_q2'%" "(`apr_jobs_q2'm)", color(gs0) size(`text_size')) ///
	text( `=`apr_q3'' `=`apr'-60' "Q3: `apr_q3'%" "(`apr_jobs_q3'm)", color(gs0) size(`text_size')) ///
	text( `=`apr_q4'' `=`apr'-60' "Q4: `apr_q4'%" "(`apr_jobs_q4'm" "jobs)", color(gs0) size(`text_size'))

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_5a.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_5a.svg")

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_apr_q1") ///
	comment("Change in Employment (%) relative to January 2020 - April 15 2020 - Income Q1") ///
	value(`=abs(`apr_q1')')

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_apr_q2") ///
	comment("Change in Employment (%) relative to January 2020 - April 15 2020 - Income Q2") ///
	value(`=abs(`apr_q2')')

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_apr_q3") ///
	comment("Change in Employment (%) relative to January 2020 - April 15 2020 - Income Q3") ///
	value(`=abs(`apr_q3')')

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_apr_q4") ///
	comment("Change in Employment (%) relative to January 2020 - April 15 2020 - Income Q4") ///
	value(`=abs(`apr_q4')')

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_dec_q1") ///
	comment("Change in Employment (%) relative to January 2020 - April 15 2022 - Income Q1") ///
	value(`=abs(`predict_q1')')

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_dec_q2") ///
	comment("Change in Employment (%) relative to January 2020 - April 15 2022 - Income Q2") ///
	value(`=abs(`predict_q2')')

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_dec_q3") ///
	comment("Change in Employment (%) relative to January 2020 - April 15 2022 - Income Q3") ///
	value(`=abs(`predict_q3')')

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_dec_q4") ///
	comment("Change in Employment (%) relative to January 2020 - April 15 2022 - Income Q4") ///
	value(`=abs(`predict_q4')')

yamlout using "${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml", ///
	key("emp_jul_q1") ///
	comment("Change in Employment (%) relative to January 2020 - July 2020 - Income Q1") ///
	value(`=abs(`jul_q1')')

project, creates("${root}/results/paper numbers/`category'/Changes in Employment by Wage Quartile.yaml")
